package androidextras.builder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;

public class FileUtils {
    public static void copyFile(File in, File out) throws IOException {
        FileChannel inChannel = new FileInputStream(in).getChannel();
        FileChannel outChannel = new FileOutputStream(out).getChannel();
        try {
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } catch (IOException e) {
            throw e;
        } finally {
            if (inChannel != null)
                inChannel.close();
            if (outChannel != null)
                outChannel.close();
        }
    }

    public static void flatten(IFile file, IProject project) {
        if (file.getFullPath().toString().contains("resx")) {
            if (file.isLinked())
                return;
            try {
                String path = file.getProjectRelativePath().toString();
                int resPos = path.indexOf("res");
                path = path.replace("resx", "res");
                int subPos = Math.max(resPos + 3,
                        Math.max(path.indexOf("/", resPos + 4), path.indexOf("\\", resPos + 4)));
                String newPath = path.substring(0, subPos);
                String newFileName = path.substring(subPos + 1).replace('\\', '_').replace('/', '_').replace('.', '_');
                // try {
                String newFile = newPath.toLowerCase() + File.separator + newFileName.toLowerCase() + '.'
                        + file.getFileExtension();

                IFile fileMoved = project.getFile(newFile);
                IFolder folder = project.getFolder(newPath.toLowerCase());
                if (!folder.exists())
                    folder.create(true, true, null);

                IPath originalPath = file.getProjectRelativePath();
                file.move(fileMoved.getFullPath(), true, null);

                IFile linkFile = project.getFile(originalPath);
                linkFile.createLink(fileMoved.getRawLocation(), 0, null);

            } catch (Exception e) {
                System.err.println("Something bad happened with " + file.getFullPath());
            }
        }

    }

    public static void replace(IFile file1, IFile file2) {
        IPath path = file1.getFullPath();
        try {
            file1.move(file2.getFullPath(), true, null);
            file2.move(path, true, null);
        } catch (CoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void delete(IFile file, IProject project) {
        if (file.getFullPath().toString().contains("resx")) {
            try {
                String path = file.getProjectRelativePath().toString();
                int resPos = path.indexOf("res");
                path = path.replace("resx", "res").replace('\\', File.separatorChar).replace('/', File.separatorChar);
                int subPos = Math.max(resPos + 3,
                        Math.max(path.indexOf("/", resPos + 4), path.indexOf("\\", resPos + 4)));
                String newPath = path.substring(0, subPos);
                String newFileName = path.substring(subPos + 1).replace('\\', '_').replace('/', '_').replace('.', '_');
                project.getFile(
                        newPath.toLowerCase() + File.separator + newFileName.toLowerCase() + '.'
                                + file.getFileExtension()).delete(true, null);
            } catch (Exception e) {
                System.err.println("Something bad happened with " + file.getFullPath());
            }
        }
    }

}
